package com.czmh.etc.common.core.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class MSignUtils {
    private MSignUtils() {
    }
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };
    private static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }
        return out;
    }

    public static String encode(String rawPass) {
        MessageDigest messageDigest = getMessageDigest();
        byte[] digest = messageDigest.digest(rawPass.getBytes());
        return new String(encodeHex(digest)).toLowerCase();
    }

    protected final static MessageDigest getMessageDigest() throws IllegalArgumentException {
        try {
            return MessageDigest.getInstance("MD5");
        }
        catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("No such algorithm [" + "MD5" + "]");
        }
    }
    public static boolean isPasswordValid(String encPass, String rawPass) {
        String pass1 = "" + encPass;
        String pass2 = encode(rawPass);
        return pass1.equals(pass2);
    }
    public static String sortParams(Map params) {
        String result = "";
        String[] args = new String[params.size()];
        Set<String> keys = params.keySet();
        Iterator<String> iterator = keys.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            args[i] = iterator.next();
            i++;
        }
        Arrays.sort(args);
        // 重组url
        for (int j = 0; j < args.length; j++) {
            String key = args[j];
            Object o = params.get(key);
            if(params.get(key)!=null&&!"".equals(o)   ){
                result += "&" + key + "=" + params.get(key);
            }
        }
        if (result.length() > 0) {
            result = result.substring(1);
        }
        return result;
    }
    public static String buildSign(HashMap<String, String> map, String appSecret) {
        String text = MSignUtils.sortParams(map);
        return MSignUtils.encode(text+appSecret );

    }
    public static void main(String[] args) {
        String result = MSignUtils.encode("111111");
        System.out.println("password=" + result.toUpperCase());
        boolean isGood = MSignUtils.isPasswordValid("E10ADC3949BA59ABBE56E057F20F883E", "111111");
        System.out.println("isGood=" + isGood);
    }
}
